<html>
<head><meta charset="utf-8"><title>Unsized Locals + FnOnce · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20Locals.20.2B.20FnOnce.html">Unsized Locals + FnOnce</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="183106792"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20Locals%20%2B%20FnOnce/near/183106792" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20Locals.20.2B.20FnOnce.html#183106792">(Dec 10 2019 at 21:54)</a>:</h4>
<p>This compiles:</p>
<div class="codehilite"><pre><span></span><span class="cp">#![feature(unsized_locals)]</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">pass_fn_once</span><span class="p">(</span><span class="n">do_thing</span>: <span class="nc">dyn</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">do_thing</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>Is there any way to actually call it though?</p>



<a name="183135159"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20Locals%20%2B%20FnOnce/near/183135159" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20Locals.20.2B.20FnOnce.html#183135159">(Dec 11 2019 at 08:07)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="cp">#![feature(unsized_locals)]</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">pass_fn_once</span><span class="p">(</span><span class="n">do_thing</span>: <span class="nc">dyn</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">do_thing</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">box_lam</span>: <span class="nb">Box</span><span class="o">&lt;</span><span class="n">dyn</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">()</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">(</span><span class="o">||</span><span class="w"> </span><span class="n">dbg</span><span class="o">!</span><span class="p">());</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">lam</span>: <span class="nc">dyn</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">()</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">box_lam</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">pass_fn_once</span><span class="p">(</span><span class="n">lam</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="183167126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20Locals%20%2B%20FnOnce/near/183167126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20Locals.20.2B.20FnOnce.html#183167126">(Dec 11 2019 at 15:21)</a>:</h4>
<p>Hmm. That feels unfortunate; is there a way to do it <em>without</em> heap allocation? I feel like I should be able to create an unsized local in <code>main</code> without it.</p>



<a name="183167266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20Locals%20%2B%20FnOnce/near/183167266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20Locals.20.2B.20FnOnce.html#183167266">(Dec 11 2019 at 15:23)</a>:</h4>
<p>I imagine you could possibly <code>&amp;mut dyn FnOnce()</code> and then cast that to <code>*mut</code> and ptr::read from that to get it onto the stack</p>



<a name="183167277"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20Locals%20%2B%20FnOnce/near/183167277" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20Locals.20.2B.20FnOnce.html#183167277">(Dec 11 2019 at 15:23)</a>:</h4>
<p>but not sure if unsized_locals and friends are good enough for that yet :)</p>



<a name="183167401"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20Locals%20%2B%20FnOnce/near/183167401" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20Locals.20.2B.20FnOnce.html#183167401">(Dec 11 2019 at 15:24)</a>:</h4>
<p>Yeah, that's always a thing I struggle with for unstable features - is that part impossible / not planned to work / doesn't work yet?</p>



<a name="183167521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20Locals%20%2B%20FnOnce/near/183167521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20Locals.20.2B.20FnOnce.html#183167521">(Dec 11 2019 at 15:26)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> best way to figure that out is to file an issue</p>



<a name="183167543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20Locals%20%2B%20FnOnce/near/183167543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20Locals.20.2B.20FnOnce.html#183167543">(Dec 11 2019 at 15:26)</a>:</h4>
<p>(don't post on the tracking issue)</p>



<a name="183167551"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20Locals%20%2B%20FnOnce/near/183167551" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20Locals.20.2B.20FnOnce.html#183167551">(Dec 11 2019 at 15:26)</a>:</h4>
<p>I, uh, disagree</p>



<a name="183167560"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20Locals%20%2B%20FnOnce/near/183167560" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20Locals.20.2B.20FnOnce.html#183167560">(Dec 11 2019 at 15:26)</a>:</h4>
<p>filing an issue to ask a question isn't great</p>



<a name="183167578"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20Locals%20%2B%20FnOnce/near/183167578" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20Locals.20.2B.20FnOnce.html#183167578">(Dec 11 2019 at 15:26)</a>:</h4>
<p>if you have a case that should work (in your opinion) but doesn't, that's another matter</p>



<a name="183167600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20Locals%20%2B%20FnOnce/near/183167600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20Locals.20.2B.20FnOnce.html#183167600">(Dec 11 2019 at 15:27)</a>:</h4>
<p>that's the way I'd ask the question</p>



<a name="183167612"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20Locals%20%2B%20FnOnce/near/183167612" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20Locals.20.2B.20FnOnce.html#183167612">(Dec 11 2019 at 15:27)</a>:</h4>
<p>I think in the future I would hope that just <code>let f: dyn FnOnce() = move || {};</code> should basically just work</p>



<a name="183167750"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Unsized%20Locals%20%2B%20FnOnce/near/183167750" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Unsized.20Locals.20.2B.20FnOnce.html#183167750">(Dec 11 2019 at 15:28)</a>:</h4>
<p>I'd go with more chat / forum avenues before I file an issue (if I ever got to an issue, I agree it feels wrong). Here or Discord are easy places, then graduate to  IRLO if not answered.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>